#!/usr/bin/env bash
# 用途：Git commit-msg 钩子入口（模板）
# 版本：v1.0.1
# 变更记录：
# - 2025-11-07：增加版本头部注释与变更记录；移除仓库名硬编码。
# - 2025-11-07：统一动态定位逻辑（本仓库 scripts/windows -> 子目录扫描 -> MIRROR_DIR_NAME）。
# 说明：动态定位 PowerShell 提交信息校验脚本，统一传入 -MsgFile "$1"。

set -e

ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"

SCRIPT=""
candidate="$ROOT/scripts/windows/validate-commit-msg.ps1"
if [ -f "$candidate" ]; then SCRIPT="$candidate"; fi

if [ -z "$SCRIPT" ]; then
  for d in "$ROOT"/*; do
    if [ -f "$d/scripts/windows/validate-commit-msg.ps1" ]; then SCRIPT="$d/scripts/windows/validate-commit-msg.ps1"; break; fi
  done
fi

if [ -z "$SCRIPT" ] && [ -f "$ROOT/config/rules.env" ]; then
  MIRROR_DIR_NAME="$(grep -E '^MIRROR_DIR_NAME=' "$ROOT/config/rules.env" | sed 's/^MIRROR_DIR_NAME=//')"
  if [ -n "$MIRROR_DIR_NAME" ] && [ -f "$ROOT/$MIRROR_DIR_NAME/scripts/windows/validate-commit-msg.ps1" ]; then
    SCRIPT="$ROOT/$MIRROR_DIR_NAME/scripts/windows/validate-commit-msg.ps1"
  fi
fi

if [ -z "$SCRIPT" ]; then echo "[hook] validate-commit-msg.ps1 not found" >&2; exit 1; fi

pwsh -NoProfile -File "$SCRIPT" -MsgFile "$1"
